//////////////////////////////////////////ok
#include"stdafx.h"

#include "bochs.h"



void IA32_CPU::AAA(Ia32_Instruction_c *)
{
  if ( ((AL & 0x0f) > 9) || get_AF() )
  {
    AX = AX + 0x106;
    set_AF(1);
    set_CF(1);
  }
  else {
    set_AF(0);
    set_CF(0);
  }

  AL = AL & 0x0f;
  set_OF(0);
  set_SF(0); /* sign is always 0 because bits 4-7 of AL are zeroed */
  set_ZF(AL == 0);
  set_PF_base(AL);
}

void IA32_CPU::AAS(Ia32_Instruction_c *)
{
  if ( ((AL & 0x0F) > 0x09) || get_AF() )
  {
    AX = AX - 0x106;
    set_AF(1);
    set_CF(1);
  }
  else {
    set_CF(0);
    set_AF(0);
  }
  AL = AL & 0x0f;
  set_OF(0);
  set_SF(0); /* sign is always 0 because bits 4-7 of AL are zeroed */
  set_ZF(AL == 0);
  set_PF_base(AL);
}

void IA32_CPU::AAM(Ia32_Instruction_c *i)
{
  Bit8u al, imm8 = i->Ib();
  if (imm8 == 0)
    exception(IA32_BX_DE_EXCEPTION, 0, 0);

  al = AL;
  AH = al / imm8;
  AL = al % imm8;
  set_OF(0);	
  set_AF(0);
  set_CF(0);
  set_SF(AL >= 0x80);
  set_ZF(AL == 0);
  set_PF_base(AL);
}

void IA32_CPU::AAD(Ia32_Instruction_c *i)
{
  Bit8u imm8 = i->Ib();
  Bit16u tmp = AH;
  tmp *= imm8;
  tmp += AL;
  AL = tmp & 0xff;
  AH = 0;
  set_OF(0);	/* undocumented flag modification */
  set_AF(0);
  set_CF(0);
  set_SF(AL >= 0x80);
  set_ZF(AL == 0);
  set_PF_base(AL);
}

void IA32_CPU::DAA(Ia32_Instruction_c *)
{
  Bit8u tmpAL = AL;
  int   tmpCF = 0;

  /* Validated against Intel Pentium family hardware. */
  // DAA affects the following flags: S,Z,A,P,C
  if (((tmpAL & 0x0F) > 0x09) || get_AF())
  {
    tmpCF = ((AL > 0xF9) || get_CF());
    AL = AL + 0x06;
    set_AF(1);
  }
  else
    set_AF(0);

  if ((tmpAL > 0x99) || get_CF())
  {
    AL = AL + 0x60;
    tmpCF = 1;
  }
  else
    tmpCF = 0;
  set_OF(0);	/* undocumented flag modification */
  set_SF(AL >= 0x80);
  set_ZF(AL==0);
  set_PF_base(AL);
  set_CF(tmpCF);
}

void IA32_CPU::DAS(Ia32_Instruction_c *)
{
  Bit8u tmpAL = AL;
  int tmpCF = 0;
  if (((tmpAL & 0x0F) > 0x09) || get_AF())
  {
    tmpCF = (AL < 0x06) || get_CF();
    AL = AL - 0x06;
    set_AF(1);
  }
  else
    set_AF(0);

  if ((tmpAL > 0x99) || get_CF())
  {
    AL = AL - 0x60;
    tmpCF = 1;
  }

  set_OF(0);	/* undocumented flag modification */
  set_SF(AL >= 0x80);
  set_ZF(AL==0);
  set_PF_base(AL);
  set_CF(tmpCF);
}
